list

type list<T> : collection<T>

Represents a mutable array list. Subtype of collection<T>.

Since

0.6.0

See also

collection

for inherited values and methods

Constructors

Link copied to clipboard
pure constructor()

Construct a new empty list.

pure constructor(values: iterable<-T>)

Construct a new list by copying the values from another iterable.

Functions

Link copied to clipboard
(alias) function _set(index: integer, value: T): T

Set the element at the specified index, overwriting the element that was previously at that index. The size of the list is unchanged.

Alias
Link copied to clipboard
(alias) function _sort()

Sort this list in place.

Alias
Link copied to clipboard
function add(value: T): boolean

Append an element to the end of this collection.

The element is not added if this collection does not allow duplicates and the element is already contained in this collection.

function add(index: integer, value: T): boolean

Insert a value at the specified index. Any elements previously occurring at and after the specified index have their indices increased by 1, and the size of the list increases by 1.

Note that for a list my_list:

  • my_list.add(my_list.size(), x) "appends" x to the end of the list

  • my_list.add(my_list.size() + 1, x) throws an exception

Note also that my_list.add(my_list.size(), x) is equivalent to my_list.add(x) (inherited from collection<T>).

Link copied to clipboard
function add_all(values: collection<-T>): boolean

Add all elements from another collection to the end of this collection.

If this collection does not allow duplicates, then only those elements not already contained in this collection are added.

function add_all(index: integer, values: collection<-T>): boolean

Insert all elements from a collection at the specified index. Any elements previously occurring at and after the specified index have their indices increased by the size of the given collection. The size of the list increases by the size of the given collection.

Note that for a list my_list:

  • my_list.add_all(my_list.size(), x) "appends" the elements of the collection x to the end of the list

  • my_list.add_all(my_list.size() + 1, x) throws an exception

Note also that my_list.add_all(my_list.size(), x) is equivalent to my_list.add_all(x) (inherited from collection<T>).

Link copied to clipboard
(alias) function addAll(values: collection<-T>): boolean

Add all elements from another collection to the end of this collection.

If this collection does not allow duplicates, then only those elements not already contained in this collection are added.

Alias
(alias) function addAll(index: integer, values: collection<-T>): boolean

Insert all elements from a collection at the specified index. Any elements previously occurring at and after the specified index have their indices increased by the size of the given collection. The size of the list increases by the size of the given collection.

Note that for a list my_list:

  • my_list.add_all(my_list.size(), x) "appends" the elements of the collection x to the end of the list

  • my_list.add_all(my_list.size() + 1, x) throws an exception

Note also that my_list.add_all(my_list.size(), x) is equivalent to my_list.add_all(x) (inherited from collection<T>).

Alias
Link copied to clipboard
function clear()

Clear this collection; i.e. remove all its elements. Immediately after this method returns, this collection is empty.

Link copied to clipboard
pure function contains(value: T): boolean

Check if this collection contains the given element.

Link copied to clipboard
pure function contains_all(values: collection<-T>): boolean

Check if this collection contains all elements of another collection.

Link copied to clipboard
(alias) pure function containsAll(values: collection<-T>): boolean

Check if this collection contains all elements of another collection.

Link copied to clipboard
pure function empty(): boolean

Check if this collection is empty.

Link copied to clipboard
pure function get(index: integer): T

Retrieve the element at the specified index.

Link copied to clipboard
pure function index_of(value: T): integer

Search for the first occurrence of the specified element within this list.

Link copied to clipboard
(alias) pure function indexOf(value: T): integer

Search for the first occurrence of the specified element within this list.

Alias
Link copied to clipboard
pure function join_to_text([separator: text], [prefix: text], [postfix: text], [limit: integer?], [truncated: text], [transform: (T) -> text]): text

Generate a textual representation of this iterable.

An optional separator, prefix and postfix can be provided. One can also provide a limit: integer?. If there are more elements in the result than limit, the elements whose indices exceed limit are omitted, and the passed truncated: text is included instead.

Examples:

  • [1, 2, 3].join_to_text() returns '1, 2, 3'.

  • [1, 2, 3].join_to_text('_') returns '1_2_3'.

  • [1, 2, 3].join_to_text('*', '(', ')') returns '(1*2*3)'.

  • list<T>().join_to_text('!', '(', ')') returns '()' (where T is a valid type).

  • range(10).join_to_text('', '', '', 5) returns '01234...'.

  • range(10).join_to_text('', '', '', 5, 'more') returns '01234more'.

Where the function even is defined:

function even(x: integer): text {
return if (x % 2 == 0) 'EVEN' else 'ODD';
}

Then:

  • range(10).join_to_text('->', '{', '}', 5, '...', even(*)) returns {EVEN->ODD->EVEN->ODD->EVEN->...}.

Link copied to clipboard
(alias) pure function len(): integer

Get the size (number of elements) of this collection.

Alias
Link copied to clipboard
function remove(value: T): boolean

Remove an element from this collection.

Link copied to clipboard
function remove_all(values: collection<-T>): boolean

Remove all elements in another collection from this collection.

Link copied to clipboard
function remove_at(index: integer): T

Remove and return the element at the specified index. The indices of elements occurring after the specified index decrease by 1, and the size of this list decreases by 1.

Link copied to clipboard
(alias) function removeAll(values: collection<-T>): boolean

Remove all elements in another collection from this collection.

Link copied to clipboard
(alias) function removeAt(index: integer): T

Remove and return the element at the specified index. The indices of elements occurring after the specified index decrease by 1, and the size of this list decreases by 1.

Alias
Link copied to clipboard
function repeat(n: integer): list<T>

Repeat this list n times.

Examples:

  • [1, 2, 3].repeat(3) returns [1, 2, 3, 1, 2, 3, 1, 2, 3]

  • list<T>().repeat(3) returns [] (for any type T)

  • [3].repeat(0) returns []

Link copied to clipboard
function reverse()

Reverses the order of the elements in the list in place.

Where this list is bound to the variable l, then the statement l.reverse(); is equivalent to l = l.reversed();.

Link copied to clipboard
function reversed(): list<T>

Returns a new list with the elements of this list in reverse order.

Examples:

  • [].reversed() returns []

  • [1].reversed() returns [1]

  • [1, 2, 3].reversed() returns [3, 2, 1]

Link copied to clipboard
function set(index: integer, value: T): T

Set the element at the specified index, overwriting the element that was previously at that index. The size of the list is unchanged.

Link copied to clipboard
pure function size(): integer

Get the size (number of elements) of this collection.

Link copied to clipboard
function sort()

Sort this list in place.

Link copied to clipboard
pure function sorted(): list<T>

Sorts the elements of this collection into a list. This collection is not modified.

Link copied to clipboard
(alias) pure function str(): text

Returns a textual representation of this collection.

Alias
Link copied to clipboard
pure function sub(start: integer): list<T>

Returns a sublist of this list starting from the specified index (inclusive).

Equivalent to list.sub(index, list.size()).

Note that:

  • my_list.sub(my_list.size() - 1) returns a list containing only the last element of my_list (assuming my_list.size > 0)

  • my_list.sub(my_list.size()) returns an empty list

  • my_list.sub(my_list.size() + 1) throws an exception

pure function sub(start: integer, end: integer): list<T>

Returns a sublist of this list starting from the specified start index (inclusive) to the specified end index (exclusive).

For the list my_list, my_list.sub(start, my_list.size()) is equivalent to my_list.sub(start).

Link copied to clipboard
pure function to_text(): text

Returns a textual representation of this collection.